<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
   <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
   <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="path" value="${pageContext.request.contextPath }" />
		
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="/WEB-INF/pages/qtop.jsp" flush="true"></jsp:include>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>在线问答系统</title>
</head>
	
<body style="background-color: white;">
	<div style="padding: 0px 200px 10px;background-color: white;">
		<form method="post" >
				<c:if test="${question.uId ne user.uId}" >
					<button type="button" onclick="collectquestion();" class="btn btn-success">收藏</button>
				</c:if>
				<c:if test="${question.uId eq user.uId}">
					<button type="button" onclick="collectquestion();" style="display: none;" class="btn btn-success">收藏</button>
				</c:if>
				
		</form>
		<div>
			<input type="hidden" value="${question.qId}" id="qId">
			<div style="font-size: 40px;font-weight: bold;padding: 20px 0px 20px 0px" >
				${question.qTitle}
			</div>
			<div  style="color:  #0ccccc; font-size: 8px;">
				<a href="" style="color: green;">${question.uName}</a>&nbsp;&nbsp;&nbsp;&nbsp;<fmt:formatDate value="${question.qApplytime}" pattern="yyyy-MM-dd HH:mm:ss"/>&nbsp;&nbsp;提问
			</div>
		</div>
		<div style="font-size: 20px;padding: 20px 0px 20px 0px">
			<textarea class="form-control" style="border:0; background-color: white;resize: none; outline: none;"   readonly="readonly">${question.qBody}</textarea>
		</div>
		<div>
			<b style="font-size: 25px">回答</b>
			<hr style="height: 3px"/>
		</div>
		
		<c:forEach items="${answerquestion}" var="answerques" varStatus="st">
			<c:if test="${answerques.aDostate eq 2 }">
				<p style="font-weight: bold;">被采纳的答案</p>
				<div style="background-color: white;">
					<div style="color:  #0ccccc; font-size: 8px;">
						<a href="" style="color: green; font-size: 8px;">${answerques.uName}</a>&nbsp;&nbsp;&nbsp;&nbsp;<fmt:formatDate value="${answerques.aApplytime}" pattern="yyyy-MM-dd HH:mm:ss"/>&nbsp;&nbsp;回答
					</div>
					<div style="font-size: 20px;padding: 10px 0px 10px 0px">
							<button type="button" class="btn btn-default btn-lg" style="color: green" disabled="disabled">已采纳</button>
						<div class="form-group">
								<textarea class="form-control" style="border:0; background-color: white;resize: none; outline: none;" onpropertychange="this.style.posHeight=this.scrollHeight"  readonly="readonly">${answerques.aBody}</textarea>
						</div>
						<hr style="height: 1px"/>
					</div>
				</div>
			</c:if>
		</c:forEach>
		
		<c:forEach items="${answerquestion}" var="answer" varStatus="st">
			<c:if test="${answer.aDostate eq 1}">
				<div style="background-color: white;">
					<div style="color:  #0ccccc; font-size: 8px;">
						<a href="" style="color: green; font-size: 8px;">${answer.uName}</a>&nbsp;&nbsp;&nbsp;&nbsp;<fmt:formatDate value="${answer.aApplytime}" pattern="yyyy-MM-dd HH:mm:ss"/>&nbsp;&nbsp;回答
					</div>
					<div style="font-size: 20px;padding: 10px 0px 10px 0px">
						<c:if test="${question.uId eq sessionScope.user.uId}">
							<c:if test="${answer.qDostate eq 1 }">
								<button type="button" onclick="cnanswer(${answer.aId});" class="btn btn-success">采纳</button>
							</c:if>
							<c:if test="${answer.qDostate ne 1 }">
								
							</c:if>
						</c:if>
						<c:if test="${question.uId ne sessionScope.user.uId }">
						
						</c:if>
							
							<div id="abody" class="form-group">
								<textarea class="form-control" style="border:0; background-color: white;resize: none; outline: none;" onpropertychange="this.style.posHeight=this.scrollHeight"  readonly="readonly">${answer.aBody}</textarea>
							</div>
							
							<hr style="height: 1px"/>
					</div>
				</div>
			</c:if>
			<input type="hidden" value="${answer.uId}" id="uId"/>
		</c:forEach>
		<b style="font-size: 25px">撰写答案</b>
		<form method="post" >
			<div class="form-group" style="padding: 10px 225px 10px 10px">
		  		<textarea style="resize: none; outline: none;" class="form-control" rows="8" id="qbody" name="qbody"></textarea>
			</div>
			<div class="pull-right" style="padding: 0px 300px 0px">
		      <div class="col-sm-offset-2 col-sm-10">
		         <button type="button" onclick="answerQuestion();" class="btn btn-default btn-lg">回答问题</button>
		      </div>
		   	</div>
		</form>
		
	</div>
	<script type="text/javascript">
		function answerQuestion(){
			var body = $('#qbody').val();
			$.ajax({
				cache : true,
   				type : "POST",
   				url : "<c:url value='/ques/answer'/>",
   				data :{"qbody":$('#qbody').val()},
   				error : function(
   						request){
   					return;
   				},
   				success : function(data){
   					if(data=="0"){
   						$('#abody').text(body);
   						window.location.href=window.location.href;
   					}else if(data=="1"){
   						alert("请先登录！再回复");
   					}
   					else if(data=="2"){
   						alert("回复内容不能为空！");
   					}
   				}
			});
		}
		
		function cnanswer(obj){
			$.ajax({
				cache : true,
   				type : "GET",
   				url : "<c:url value='/ques/cnanswer'/>",
   				data :{"aId":obj,"qId":$('#qId').val(),"uId":$('#uId').val()},
   				error : function(
   						request){
   					alert(2);
   					return;
   				},
   				success : function(data){
   					if(data==0){
   						alert("答案采纳成功!");
   						window.location.href=window.location.href;
   					}
   				}
			});
		}
		
		function collectquestion(){
			$.ajax({
				cache : true,
   				type : "POST",
   				url : "<c:url value='/ques/collectques/${question.qId}'/>",
   				error : function(
   						request){
   					return;
   				},
   				success : function(data){
   					if(data=="0"){
						alert("收藏成功！");   						 
   					}else if(data=="1"){
   						alert("该问题你已经收藏过了");
   					}else if(data=="2"){
   						alert("请先登录！再收藏");
   					}
   				}
			});
		}
	</script>
</body>
</html>